define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'lease/review/index' + location.search,
                    add_url: 'lease/review/add',
                    // edit_url: 'lease/review/edit',
                    del_url: 'lease/review/del',
                    multi_url: 'lease/review/multi',
                    import_url: 'lease/review/import',
                    table: 'lease_review',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        { checkbox: true },
                        { field: 'id', title: __('Id') },
                        { field: 'user.nickname', title: __('User.nickname'), operate: 'LIKE' },
                        { field: 'lease.title', title: __('Lease.title'), operate: 'LIKE' },
                        { field: 'content', title: __('Content'),  cellStyle:function () {
                                return {
                                    css:{
                                        "max-width":'300px',
                                        "text-overflow":'ellipsis',
                                        "white-space":'initial',
                                    }
                                }
                        }},
                        { field: 'images', title: __('Images'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.images },
                        { field: 'star', title: __('Star') },
                        { field: 'createtime', title: __('Createtime'), operate: 'RANGE', addclass: 'datetimerange', autocomplete: false, formatter: Table.api.formatter.datetime },
                        { field: 'state', title: __('审核'), operate: false, formatter: Controller.api.formatter.state },
                        {
                            field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate,
                            buttons: [
                                {
                                    name: 'query',
                                    title: '查看',
                                    text: '查看',
                                    classname: 'btn btn-primary btn-xs btn-view btn-dialog',
                                    url: 'lease/review/query',//接口地址 控制器名/方法名
                                    visible: function (row) {//判断显示隐藏 只有状态为待审核时展示操作按钮
                                        if (row.recover == '1') {
                                            return true;//显示
                                        } else {
                                            return false;//隐藏
                                        }

                                    },
                                },
                                {
                                    name: 'recover',
                                    title: '回复',
                                    text: '回复',
                                    classname: 'btn btn-primary btn-xs btn-view btn-dialog',
                                    url: 'lease/review/recover',//接口地址 控制器名/方法名
                                    visible: function (row) {//判断显示隐藏 只有状态为待审核时展示操作按钮
                                        if (row.recover == '0') {
                                            return true;//显示
                                        } else {
                                            return false;//隐藏
                                        }
                                    },
                                },
                            ]
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        
        recover: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            },
            formatter: {//渲染的方法
                state: function (value, row, index) {
                    //添加上btn-change可以自定义请求的URL进行数据处理
                    return '<a class="btn-change text-success" data-url="lease/review/examine" data-confirm="确认审核？" data-id="' + row.id + '"><i class="fa ' + (row.state == 1 ? 'fa-toggle-on fa-flip-horizontal text-gray' : 'fa-toggle-on') + ' fa-2x"></i></a>';
                },
            },
        }
    };
    return Controller;
});
